package cn.edu.nuc.creditSystem.service.credit.impl;

import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.nuc.creditSystem.bean.credit.Account;
import cn.edu.nuc.creditSystem.bean.credit.Authentication;
import cn.edu.nuc.creditSystem.bean.user.User;
import cn.edu.nuc.creditSystem.dao.BaseMapper;
import cn.edu.nuc.creditSystem.dao.credit.inf.AccountMapper;
import cn.edu.nuc.creditSystem.dao.credit.inf.AuthenticationMapper;
import cn.edu.nuc.creditSystem.dao.user.inf.UserMapper;
import cn.edu.nuc.creditSystem.service.BaseImpl;
import cn.edu.nuc.creditSystem.service.credit.service.AuthenticationService;
import cn.edu.nuc.creditSystem.utils.SendEmail;
@Service
public class AuthenticationImpl extends BaseImpl<Authentication> implements AuthenticationService{
	@Autowired
	private AuthenticationMapper am;
	@Autowired
	private UserMapper um;
	@Autowired
	private AccountMapper accountMapper;
	@Override
	public BaseMapper<Authentication> getBaseDao() {
		return am;
	}

	@Override
	public Authentication getByUserId(Integer userid) {
		return am.getByUserId(userid);
	}

	@Override
	@Transactional
	public Boolean AuthenticationPass(Authentication a) {
		am.update(a);
		User user=new User();
		user.setId(a.getUserid());
		user.setRoleId(2);
		um.update(user);
		Account account=new Account();
		account.setId(a.getUserid());
		accountMapper.insert(account);
		return true;
	}

	@Override
	public void setEmail(Authentication param) {
		StringBuffer sb=new StringBuffer("点击下面链接激活账号，请尽快认证！</br>");  
        sb.append("<a http://localhost:8080/CreditSystem/cs/credit/pass?state=1&id="+param.getId()+"&userid="+param.getUserid());  
        sb.append("\">");
        sb.append("</a>");  
        SendEmail.send(param.getEmail(), sb.toString());  
	}

	@Override
	public void setPhone(Authentication param) throws Exception {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn");
		// PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
		NameValuePair[] data = { new NameValuePair("Uid", "dengsilinming"),// 注册的用户名
				new NameValuePair("Key", "72da78da5ff54f450505"),// 注册成功后，登录网站后得到的密钥
				new NameValuePair("smsMob", param.getPhone()),// 手机号码
				new NameValuePair("smsText", "这是专用于测试的信息，能否正常发短信呢？") };// 短信内容
		post.setRequestBody(data);

		client.executeMethod(post);
		Header[] headers = post.getResponseHeaders();
		int statusCode = post.getStatusCode();
		System.out.println("statusCode:" + statusCode);
		for (Header h : headers) {
			System.out.println("---" + h.toString());
		}
		String result = new String(post.getResponseBodyAsString().getBytes(
				"gbk"));
		System.out.println(result);
	}

}
